<?php
session_start();

require_once "../dao/blog_dao.php";
require_once "../dao/store_dao.php";
require_once "../../app/dao/database.php";


$modul=$_GET['modul'];
$id=$_GET['id'];
//$modul="tambah";
$class_db = new Database;


switch($modul){
    
    case "hapus":
    
        $blog = new Blog_Dao($class_db);
        
     	$blog->del($id);
        
        header('location:blog_view.php');
            
    break; 
    
    case "publish":
    
        $blog = new Blog_Dao($class_db);
        
        $query = $blog->publish($id);
        
        header('location:blog_view.php');
    
    break;
    
    case "tambah":
        //echo "test";
        $blog = new Blog_Dao($class_db);
        $tgl = date('Y-m-d');
        //print_r($tgl."</br>");
        $title = $_POST['title'];
        //print_r($title."</br>");
        $content = $_POST['content'];
        //print_r($content."</br>");
        $dir_file = $_FILES['image']['tmp_name'];
        //print_r($dir_file."</br>");
        $tipe_file      = $_FILES['image']['type'];
        //print_r($tipe_file."</br>");
        $nama_file      = $_FILES['image']['name'];
        //print_r($nama_file."\n</br>");
        $store = new Store_Dao($class_db);
        //print_r($_SESSION['loggedinuser']);
        $contributor = $store->get_store_name($_SESSION['loggedinuser']);
        print_r($contributor);
        
        $value = array("$tgl","$title","$content","$nama_file","$contributor");
        print_r($value);                
        //print_r($value);
        
        $row = 'date,
                        title,
                        content,
                        image,
                        contributor';
        
        //print_r($row);
                
        $result = $class_db->add('blog',$value, $row);
        $dir_blog = '../../images/blog/';
        $file_to = $dir_blog.$nama_file;
                
        //memindah file
        move_uploaded_file($dir_file, $file_to);
        
        if($result){
            header('location:blog_view.php');
        }else{
            echo "gagal menyimpan!";
        }
    
    
    break;
    
    
    }